<template>
  <div class="box">
    <div class="scroll-view">
      <projectMsg :projectId="project_id" :hrefProject="true"></projectMsg>
      <div class="user-title-box">基础信息</div>
      <el-form inline :model="tjkcxxRuleForm" :rules="tjkcxxRules" ref="tjkcxxRuleForm" label-position="top">
        <el-form-item label="现场图片 (.jpg,.png,.gif)" prop="scene_file">
          <div class="file-list-box">
            <el-upload accept="image/*" multiple class="avatar-uploader" action="#" :show-file-list="false"
              :auto-upload="false" list-type="picture-card" :on-change="(file, fileList) => {
                tjkcxxFileChange(file, fileList, 'scene_file');
              }
                ">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div class="file-item-box" v-for="(item, index) in tjkcxxRuleForm.scene_file" :key="index">
              <div class="el-icon-delete-box" @click="tjkcxxFileRemove(index, 'scene_file')">
                <i class="el-icon-error"></i>
              </div>
              <el-tooltip class="item" effect="dark" :content="item.name || item.file_name" placement="top">
                <img class="img" :src="$fileImgFunc(item)" />
              </el-tooltip>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="勘察报告 (.jpg,.png,.gif,.xlsx,.xls)" prop="report_file">
          <div class="file-list-box">
            <el-upload accept="image/*,.xlsx,.xls" multiple class="avatar-uploader" action="#" :show-file-list="false"
              :auto-upload="false" list-type="picture-card" :on-change="(file, fileList) => {
                tjkcxxFileChange(file, fileList, 'report_file');
              }
                ">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div class="file-item-box" v-for="(item, index) in tjkcxxRuleForm.report_file" :key="index">
              <div class="el-icon-delete-box" @click="tjkcxxFileRemove(index, 'report_file')">
                <i class="el-icon-error"></i>
              </div>
              <el-tooltip class="item" effect="dark" :content="item.name || item.file_name" placement="top">
                <img class="img" :src="$fileImgFunc(item)" />
              </el-tooltip>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="视频 (.mp4,.3GP,.WMV)" prop="video_file">
          <div class="file-list-box">
            <el-upload accept="video/*" multiple class="avatar-uploader" action="#" :show-file-list="false"
              :auto-upload="false" list-type="picture-card" :on-change="(file, fileList) => {
                tjkcxxFileChange(file, fileList, 'video_file');
              }
                ">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div class="file-item-box" v-for="(item, index) in tjkcxxRuleForm.video_file" :key="index">
              <div class="el-icon-delete-box" @click="tjkcxxFileRemove(index, 'video_file')">
                <i class="el-icon-error"></i>
              </div>
              <el-tooltip class="item" effect="dark" :content="item.name || item.file_name" placement="top">
                <img class="img" :src="$fileImgFunc(item)" />
              </el-tooltip>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="手绘图 (.jpg,.png,.gif)" prop="sketch_file">
          <div class="file-list-box">
            <el-upload accept="image/*" multiple class="avatar-uploader" action="#" :show-file-list="false"
              :auto-upload="false" list-type="picture-card" :on-change="(file, fileList) => {
                tjkcxxFileChange(file, fileList, 'sketch_file');
              }
                ">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div class="file-item-box" v-for="(item, index) in tjkcxxRuleForm.sketch_file" :key="index">
              <div class="el-icon-delete-box" @click="tjkcxxFileRemove(index, 'sketch_file')">
                <i class="el-icon-error"></i>
              </div>
              <el-tooltip class="item" effect="dark" :content="item.name || item.file_name" placement="top">
                <img class="img" :src="$fileImgFunc(item)" />
              </el-tooltip>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input class="input-box" v-model="tjkcxxRuleForm.remark" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="tjkcxxSubmit('tjkcxxRuleForm')" :disabled="diaLoading"
          :loading="diaLoading">提交</el-button>
        <el-button @click="tjkcxxCancel('tjkcxxRuleForm')">关闭</el-button>
      </span>
    </div>
  </div>
</template>
<script>
import projectApi from "@/api/projectApi";
import projectMsg from "@/components/ProjectMsg";

export default {
  name: "tjkcbgCom",
  components: {
    projectMsg,
  },
  data() {
    var file1 = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请上传现场图片"));
      } else {
        callback();
      }
    };
    var file2 = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请上传勘察报告"));
      } else {
        callback();
      }
    };
    var file3 = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请上传视频"));
      } else {
        callback();
      }
    };
    var file4 = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请上传手绘图"));
      } else {
        callback();
      }
    };
    return {
      tjkcxxRuleForm: {
        scene_file: [],
        report_file: [],
        video_file: [],
        sketch_file: [],
        remark: "",
      },
      tjkcxxRules: {
        scene_file: {
          required: true,
          validator: file1,
          trigger: "change",
        },
        report_file: {
          required: true,
          validator: file2,
          trigger: "change",
        },
        video_file: {
          required: true,
          validator: file3,
          trigger: "change",
        },
        sketch_file: {
          required: true,
          validator: file4,
          trigger: "change",
        },
      },
      diaLoading: false,
      project_id: "",
    };
  },
  computed: {
    hisList() {
      return this.$store.getters.routerHis;
    },
    path() {
      return this.$route.path;
    },
  },
  mounted() {
    this.project_id = this.$route.query.id;
  },
  methods: {
    // 关闭提交勘场报告弹窗关闭
    tjkcxxCancel(formName) {
      this.$goBackTab(this);
    },
    // 提交勘场报告
    tjkcxxSubmit(formName) {
      let tjkcxxRuleForm = this.tjkcxxRuleForm;
      console.log(tjkcxxRuleForm, "tjkcxxRuleForm");

      this.$refs[formName].validate((valid) => {
        if (valid) {
          let formdata = new FormData();
          tjkcxxRuleForm.scene_file.forEach((item) => {
            formdata.append("scene_file[]", item.raw);
          });
          tjkcxxRuleForm.report_file.forEach((item) => {
            formdata.append("report_file[]", item.raw);
          });
          tjkcxxRuleForm.video_file.forEach((item) => {
            formdata.append("video_file[]", item.raw);
          });
          tjkcxxRuleForm.sketch_file.forEach((item) => {
            formdata.append("sketch_file[]", item.raw);
          });
          formdata.append(
            "remark",
            JSON.stringify({
              remark: tjkcxxRuleForm.remark,
              project_id: this.project_id,
            })
          );
          this.diaLoading = true;
          projectApi
            .submitSurveyInformation(formdata)
            .then((res) => {
              console.log(res, "提交勘场报告");
              this.diaLoading = false;
              if (res.code == 200) {
                this.tjkcxxCancel();
              }
              this.$message({
                type: res.code == 200 ? "success" : "warning",
                message: res.code == 200 ? "处理成功" : res.message,
              });
            })
            .catch((err) => {
              this.diaLoading = false;
              this.$message({
                type: "warning",
                message: err.message,
              });
            });
        } else {
          return false;
        }
      });
    },
    // 提交勘场信息文件选择
    tjkcxxFileChange(file, fileList, type) {
      console.log(fileList, type);
      let tjkcxxRuleForm = this.tjkcxxRuleForm;
      fileList.forEach((item) => {
        item.show = URL.createObjectURL(item.raw);
      });
      switch (type) {
        // 现场图片
        case "scene_file":
          tjkcxxRuleForm.scene_file = fileList;
          break;
        // 勘察报告
        case "report_file":
          tjkcxxRuleForm.report_file = fileList;
          break;
        // 视频
        case "video_file":
          tjkcxxRuleForm.video_file = fileList;
          break;
        // 手绘图
        case "sketch_file":
          tjkcxxRuleForm.sketch_file = fileList;
          break;
        default:
      }
      this.tjkcxxRuleForm = tjkcxxRuleForm;
    },
    // 提交勘场信息文件选择（删除）
    tjkcxxFileRemove(index, type) {
      console.log(index, type);
      let tjkcxxRuleForm = this.tjkcxxRuleForm;
      switch (type) {
        // 现场图片
        case "scene_file":
          tjkcxxRuleForm.scene_file.splice(index, 1);
          break;
        // 勘察报告
        case "report_file":
          tjkcxxRuleForm.report_file.splice(index, 1);
          break;
        // 视频
        case "video_file":
          tjkcxxRuleForm.video_file.splice(index, 1);
          break;
        // 手绘图
        case "sketch_file":
          tjkcxxRuleForm.sketch_file.splice(index, 1);
          break;
        default:
      }
      this.tjkcxxRuleForm = tjkcxxRuleForm;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/projectOparet.scss";
@import "@/styles/addPage.scss";
</style>